package org.outlook.test.ui.impl;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.Timer;

import javax.swing.Box;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.event.CaretEvent;
import javax.swing.text.html.HTMLEditorKit;

public class TalkPane extends JSplitPane implements ActionListener {

	private static final long serialVersionUID = 825178457346177512L;
	private static final String COLOR_BUTTON_SIZE = "5"; //$NON-NLS-1$
	private static final String COLOR_BUTTON_TEXT = "A"; //$NON-NLS-1$
	
	private JEditorPane talkArea;
	private JTextArea newMessageArea;
	private JScrollPane talkAreaPane;
	private JScrollPane messageAreaPane;
	private JPanel newMessagePane;
	private JToolBar messageToolbar;
	private JButton colorButton;
	private JButton sendButton;
	private JButton underlineButton;
	private JButton italicButton;
	private JButton boldButton;
	private JButton escapeXMLButton;
	private JButton bellButton;
	private JComboBox smileyCBox;
	private JComboBox sizeComboBox;
	private JButton fontButton;
	private JButton attachmentButton;
	
	private String[] history = null; 
	private int historyIndex = 0; 
	private int historyCallIndex = 0; 
	
	private boolean underline = false;
	private boolean bold = false;
	private boolean italic = false;
	private File attachment = null;
	
	public TalkPane() {
		super();
		init();
		history = new String[10];
		historyIndex = 0;
		historyCallIndex = 0;
	}

	private void init() {

		setOrientation(JSplitPane.VERTICAL_SPLIT);
		{
			talkAreaPane = new JScrollPane();
			add(talkAreaPane, JSplitPane.TOP);
			{
				talkArea = new JEditorPane();
				talkArea.setEditorKit(new HTMLEditorKit());
				talkArea.setEditable(false);
				talkAreaPane.setViewportView(talkArea);
				talkArea.setText(HTMLConstants.HTML + HTMLConstants.ENDHTML);
				talkAreaPane.setPreferredSize(new java.awt.Dimension(310, 200));
			}
		}
		{
			newMessagePane = new JPanel();
			add(newMessagePane, JSplitPane.BOTTOM);
			BorderLayout lLayout = new BorderLayout();
			newMessagePane.setLayout(lLayout);
			{
				messageToolbar = new JToolBar();
				messageToolbar.setFloatable(false);
				messageToolbar.setRollover(true);
				newMessagePane.add(messageToolbar, BorderLayout.NORTH);
				{
					boldButton = new JButton();
					boldButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("icon/bold.png"))); //$NON-NLS-1$
					boldButton.addActionListener(this);
					boldButton.setActionCommand(TalkPaneActionCommand.BOLD);
			        messageToolbar.addSeparator();
					messageToolbar.add(boldButton);
				}
				{
					italicButton = new JButton();
					italicButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("icon/italic.png"))); //$NON-NLS-1$
					italicButton.addActionListener(this);
					italicButton.setActionCommand(TalkPaneActionCommand.ITALIC);
			        messageToolbar.addSeparator();
					messageToolbar.add(italicButton);
				}
				{
					underlineButton = new JButton();
					underlineButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("icon/underline.png"))); //$NON-NLS-1$
					underlineButton.addActionListener(this);
					underlineButton.setActionCommand(TalkPaneActionCommand.UNDERLINE);
			        messageToolbar.addSeparator();
					messageToolbar.add(underlineButton);
				}
				{
					colorButton = new JButton();
					colorButton.setText(HTMLConstants.HTML + HTMLConstants.FONTCOLOR 
						+ HTMLConstants.FONTSIZE 
						+ COLOR_BUTTON_SIZE + HTMLConstants.TAGEND + HTMLConstants.BOLD + COLOR_BUTTON_TEXT
						+ HTMLConstants.ENDBOLD + HTMLConstants.ENDFONT + HTMLConstants.ENDHTML);
					colorButton.setMaximumSize(new Dimension(20, 20));
					colorButton.setMinimumSize(new Dimension(20, 20));
					colorButton.addActionListener(this); 
					colorButton.setActionCommand(TalkPaneActionCommand.COLOR);
					messageToolbar.addSeparator();
					messageToolbar.add(colorButton);
				}
				{
					fontButton = new JButton();
					fontButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("icon/font.png"))); //$NON-NLS-1$
					fontButton.addActionListener(this); 
					fontButton.setActionCommand(TalkPaneActionCommand.FACE);
					messageToolbar.addSeparator();
					messageToolbar.add(fontButton);
				}
				{
					ComboBoxModel sizeComboBoxModel = 
						new DefaultComboBoxModel(
								new String[] { "1", "2", "3", "4", "5", "6", "7" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
					sizeComboBox = new JComboBox();
					sizeComboBox.setModel(sizeComboBoxModel);
					sizeComboBox.setSelectedIndex(1);
					sizeComboBox.setMaximumSize(new Dimension(40, 22));
					sizeComboBox.addActionListener(this);
					sizeComboBox.setActionCommand(TalkPaneActionCommand.SIZE);
			        messageToolbar.addSeparator();
					messageToolbar.add(sizeComboBox);
				}
				{
					Integer[] intArray = new Integer[10];
					for (int i = 0; i < 10; i++) {
						intArray[i] = new Integer(i);
					}
					smileyCBox = new JComboBox(intArray);
					smileyCBox.setRenderer(new SmileyComboBoxRenderer(true));
			        smileyCBox.setMaximumRowCount(5);
			        smileyCBox.setMaximumSize(new Dimension(80, 22));
			        smileyCBox.addActionListener(this);
			        smileyCBox.setActionCommand(TalkPaneActionCommand.SMILEY);
			        messageToolbar.addSeparator();
					messageToolbar.add(smileyCBox);
				}
				{
					escapeXMLButton = new JButton();
					escapeXMLButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("icon/xml_accept.png"))); //$NON-NLS-1$
					escapeXMLButton.addActionListener(this);
					escapeXMLButton.setActionCommand(TalkPaneActionCommand.AUTO_ESCAPE);
			        messageToolbar.addSeparator();
					messageToolbar.add(escapeXMLButton);
				}
				{
					bellButton = new JButton();
					bellButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("icon/sound.png"))); //$NON-NLS-1$
					bellButton.addActionListener(this);
					bellButton.setActionCommand(TalkPaneActionCommand.SOUND);
			        messageToolbar.addSeparator();
					messageToolbar.add(bellButton);
				}
				{
					attachmentButton = new JButton();
					attachmentButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("icon/attachment.png"))); //$NON-NLS-1$
					attachmentButton.addActionListener(this);
					attachmentButton.setActionCommand(TalkPaneActionCommand.ATTACHMENT);
			        messageToolbar.addSeparator();
					messageToolbar.add(attachmentButton);
				}
				{
					sendButton = new JButton();
					sendButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("icon/next.png"))); //$NON-NLS-1$
					sendButton.addActionListener(this);
					sendButton.setActionCommand(TalkPaneActionCommand.SEND);
					messageToolbar.add(Box.createHorizontalGlue());
					messageToolbar.add(sendButton);
				}
			}
			{
				messageAreaPane = new JScrollPane();
				newMessagePane.add(messageAreaPane, BorderLayout.CENTER);
				{
					newMessageArea = new JTextArea();
					messageAreaPane.setViewportView(newMessageArea);
					newMessageArea.setLineWrap(true);
					newMessageArea.setWrapStyleWord(true);
					newMessageArea.addMouseListener(new EscapeXMLCharMouseListener(this));
				}
			}
		}
		newMessageArea.requestFocus();
	}

	public void actionPerformed(ActionEvent e) {
		
		if (e.getActionCommand() == TalkPaneActionCommand.SEND) {
			send();
			newMessageArea.requestFocus();
		}
		else if (e.getActionCommand() == TalkPaneActionCommand.COLOR) {
		    Color newColor = JColorChooser.showDialog(
		    		TalkPane.this,
		            "LANSLIM.99", //$NON-NLS-1$
		            Color.black);
			 if (newColor != null) {
			 }
	    	 newMessageArea.requestFocus();
		}
		else if (e.getActionCommand() == TalkPaneActionCommand.SIZE) {
		}
		else if (e.getActionCommand() == TalkPaneActionCommand.FACE) {
		}
		else if (e.getActionCommand() == TalkPaneActionCommand.BOLD) {
		}
		else if (e.getActionCommand() == TalkPaneActionCommand.ITALIC) {
		}
		else if (e.getActionCommand() == TalkPaneActionCommand.UNDERLINE) {
		}
		else if (e.getActionCommand() == TalkPaneActionCommand.SMILEY) {
    		int lPos = newMessageArea.getSelectionStart();
			String before = newMessageArea.getText().substring(0, lPos);
			String after = newMessageArea.getText().substring(lPos);
    		newMessageArea.setText(before + ":)" + after);
    		newMessageArea.setCaretPosition(lPos 
    				+ ":)".length());
    		newMessageArea.requestFocus();
		}
		else if (e.getActionCommand() == TalkPaneActionCommand.ESCAPE_CHAR) {
			escapeXMLChar();
			newMessageArea.requestFocus();
		}
		else if (e.getActionCommand() == TalkPaneActionCommand.AUTO_ESCAPE) {
		}
		else if (e.getActionCommand() == TalkPaneActionCommand.SOUND) {
		}
		else if (e.getActionCommand() == TalkPaneActionCommand.ATTACHMENT) {
			if (attachment == null) {
				
				JFileChooser fc = new JFileChooser();
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				int returnVal = fc.showOpenDialog(this);
		        if (returnVal == JFileChooser.APPROVE_OPTION && fc.getSelectedFile() != null) {
		        	attachment = fc.getSelectedFile();
					attachmentButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("icon/attachment_cancel.png"))); //$NON-NLS-1$
				}
			}
			else {
				attachment = null;
				attachmentButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("icon/attachment.png"))); //$NON-NLS-1$
			}
			newMessageArea.requestFocus();
		}

	}

	protected static String toDoubleHex(int i) {
		String result = Integer.toHexString(i);
		if (result.length() == 1) {
			return ("0" + result); //$NON-NLS-1$
		}
		return result.toUpperCase();
	}

	private void send() {
		try {
			String lMessage = newMessageArea.getText();
			attachment = null;
			attachmentButton.setIcon(new ImageIcon(ClassLoader.getSystemResource("icon/attachment.png"))); //$NON-NLS-1$
			history[historyIndex] = lMessage;
			if (historyIndex < 9) {
				historyIndex++;
			}
			else {
				historyIndex = 0;
			}
			historyCallIndex = historyIndex;
			newMessageArea.setText("");
			
//			Element e = new 
			talkArea.setText(lMessage+", Test rich test, haha...,<img src='"+ClassLoader.getSystemResource("icon/3.png")+"'></img>");
		}
		catch (Exception ioe) {
			JOptionPane.showMessageDialog(getRootPane().getParent(),
			    "LANSLIM.45", //$NON-NLS-1$ //$NON-NLS-2$
			    "LANSLIM.22", //$NON-NLS-1$
			    JOptionPane.ERROR_MESSAGE);
		}
		newMessageArea.requestFocus();
	}
	
//	public void notifyTextTalkUpdate() {
//		talkArea.setText(HTMLConstants.HTML + "testststst" + HTMLConstants.ENDHTML);
//		talkArea.setCaretPosition(talkArea.getDocument().getLength());
//		
//		if (talkModel.getPeopleIn().size() <= 1) {
//			newMessageArea.setEnabled(false);
//			sendButton.setEnabled(false);
//			colorButton.setEnabled(false);
//			colorButton.setText(HTMLConstants.HTML + HTMLConstants.FONTCOLOR 
//					+ HTMLConstants.GREY + HTMLConstants.FONTSIZE 
//					+ COLOR_BUTTON_SIZE + HTMLConstants.TAGEND + HTMLConstants.BOLD + COLOR_BUTTON_TEXT
//					+ HTMLConstants.ENDBOLD + HTMLConstants.ENDFONT + HTMLConstants.ENDHTML);
//			underlineButton.setEnabled(false);
//			italicButton.setEnabled(false);
//			boldButton.setEnabled(false);
//	        smileyCBox.setRenderer(new SmileyComboBoxRenderer(false));
//			smileyCBox.setEnabled(false);
//			sizeComboBox.setEnabled(false);
//		}
//		else {
//			newMessageArea.setEnabled(true);
//			sendButton.setEnabled(true);
//			colorButton.setEnabled(true);
//			colorButton.setText(HTMLConstants.HTML + HTMLConstants.FONTCOLOR 
//					+ talkModel.getMessageFontColor() + HTMLConstants.FONTSIZE 
//					+ COLOR_BUTTON_SIZE + HTMLConstants.TAGEND + HTMLConstants.BOLD + COLOR_BUTTON_TEXT
//					+ HTMLConstants.ENDBOLD + HTMLConstants.ENDFONT + HTMLConstants.ENDHTML);
//			underlineButton.setEnabled(true);
//			italicButton.setEnabled(true);
//			boldButton.setEnabled(true);
//	        smileyCBox.setRenderer(new SmileyComboBoxRenderer(true));
//			smileyCBox.setEnabled(true);
//			sizeComboBox.setEnabled(true);
//		}
//	}

	public void keyPressed(KeyEvent e) {
		
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
	        if ((e.getModifiers() & InputEvent.ALT_MASK) != 0) {
        		int lPos = newMessageArea.getSelectionStart();
				String before = newMessageArea.getText().substring(0, lPos);
				String after = newMessageArea.getText().substring(lPos);
				newMessageArea.setText(before + '\n' + after);
				newMessageArea.setCaretPosition(lPos + "\n".length());
	        }
	        else {
	        	send();		        
        	}
		}
		else if (e.getKeyCode() == KeyEvent.VK_PAGE_UP) {
			if (historyCallIndex == 0) {
				historyCallIndex = 9;
	        }
			else {
				historyCallIndex--;
			}
			newMessageArea.setText(history[historyCallIndex]);
		}
		else if (e.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {
			if (historyCallIndex == 9) {
				historyCallIndex = 0;
	        }
			else {
				historyCallIndex++;
			}
			newMessageArea.setText(history[historyCallIndex]);
		}
		else if (e.getKeyCode() == KeyEvent.VK_B
				&& ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) {
			styleButtonAction(bold, HTMLConstants.BOLD, HTMLConstants.ENDBOLD);
			bold = !bold;
			newMessageArea.requestFocus();
		}
		else if (e.getKeyCode() == KeyEvent.VK_I
				&& ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) {
			styleButtonAction(italic, HTMLConstants.ITALIC, HTMLConstants.ENDITALIC);
			italic = !italic;
			newMessageArea.requestFocus();
		}
		else if (e.getKeyCode() == KeyEvent.VK_U
				&& ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) {
			styleButtonAction(underline, HTMLConstants.UNDERLINE, HTMLConstants.ENDUNDERLINE);
			underline = !underline;
			newMessageArea.requestFocus();
		}
		else if (e.getKeyCode() == KeyEvent.VK_F1) {
			insertShortcut(0);
		}
		else if (e.getKeyCode() == KeyEvent.VK_F2) {
			insertShortcut(1);
		}
		else if (e.getKeyCode() == KeyEvent.VK_F3) {
			insertShortcut(2);
		}
		else if (e.getKeyCode() == KeyEvent.VK_F4) {
			insertShortcut(3);
		}
		else if (e.getKeyCode() == KeyEvent.VK_F5) {
			insertShortcut(4);
		}
		else if (e.getKeyCode() == KeyEvent.VK_F6) {
			insertShortcut(5);
		}
		else if (e.getKeyCode() == KeyEvent.VK_F7) {
			insertShortcut(6);
		}
		else if (e.getKeyCode() == KeyEvent.VK_F8) {
			insertShortcut(7);
		}
		else if (e.getKeyCode() == KeyEvent.VK_F9) {
			insertShortcut(8);
		}
		else if (e.getKeyCode() == KeyEvent.VK_F10) {
			insertShortcut(9);
		}
		else if (e.getKeyCode() == KeyEvent.VK_F11) {
			insertShortcut(10);
		}
		else if (e.getKeyCode() == KeyEvent.VK_F12) {
			insertShortcut(11);
		}
	}

	
	private void insertShortcut(int i) {

		if (newMessageArea.getSelectedText() == null) {
			int lPos = newMessageArea.getCaret().getDot();
			String before = newMessageArea.getText().substring(0, lPos);
			String after = newMessageArea.getText().substring(lPos);
			newMessageArea.setText(before + "talkModel.getShortcut(i)" + after);
			newMessageArea.setCaretPosition(lPos + "talkModel.getShortcut(i)".length());
		}
		else {
			int start = newMessageArea.getSelectionStart();
			int end = newMessageArea.getSelectionEnd();
			String before = newMessageArea.getText().substring(0, start);
			String after = newMessageArea.getText().substring(end);
			newMessageArea.setText(before + "talkModel.getShortcut(i)" + after);
			newMessageArea.setCaretPosition(start + "talkModel.getShortcut(i)".length());
		}
	}
	
 	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			if ((e.getModifiers() & InputEvent.ALT_MASK) == 0) {
				newMessageArea.setText("");
			}
		}
	}

	public void keyTyped(KeyEvent e) {
		// nothing to do here
	}

	protected void export(File pFile) throws IOException {
	}


	public void caretUpdate(CaretEvent e) {
		
		String s = newMessageArea.getText().substring(0, Math.min(e.getDot(), e.getMark()));
		bold = s.lastIndexOf(HTMLConstants.BOLD) > s.lastIndexOf(HTMLConstants.ENDBOLD);
		italic = s.lastIndexOf(HTMLConstants.ITALIC) > s.lastIndexOf(HTMLConstants.ENDITALIC);
		underline = s.lastIndexOf(HTMLConstants.UNDERLINE) > s.lastIndexOf(HTMLConstants.ENDUNDERLINE);
	}

	private void styleButtonAction(boolean currValue, String ifFalse, String ifTrue) {

		if (newMessageArea.getSelectedText() == null) {
			int start = newMessageArea.getCaret().getDot();
			String before = newMessageArea.getText().substring(0, start);
			String after = newMessageArea.getText().substring(start);
			if (!currValue) {
				newMessageArea.setText(before + ifFalse + after);
				newMessageArea.setCaretPosition(start + ifFalse.length());
			}
			else {
				newMessageArea.setText(before + ifTrue + after);
				newMessageArea.setCaretPosition(start + ifTrue.length());
			}
        }
		else {
			int start = newMessageArea.getSelectionStart();
			int end = newMessageArea.getSelectionEnd();
			String before = newMessageArea.getText().substring(0, start);
			String after = newMessageArea.getText().substring(end);
			if (!currValue) {
				newMessageArea.setText(before + ifFalse + newMessageArea.getSelectedText() + ifTrue + after);
			}
			else {
				newMessageArea.setText(before + ifTrue + newMessageArea.getSelectedText() + ifFalse + after);
			}
			newMessageArea.setCaretPosition(end + ifFalse.length() + ifTrue.length());
		}
	}
	
	private class TalkPaneActionCommand {

		public static final String FACE = "face"; //$NON-NLS-1$

		public static final String COLOR = "color"; //$NON-NLS-1$
		
		public static final String BOLD = "bold"; //$NON-NLS-1$

		public static final String ITALIC = "italic"; //$NON-NLS-1$

		public static final String UNDERLINE = "underline"; //$NON-NLS-1$
		
		public static final String SEND = "send"; //$NON-NLS-1$
		
		public static final String SMILEY = "smiley"; //$NON-NLS-1$

		public static final String SIZE = "size"; //$NON-NLS-1$

		public static final String ESCAPE_CHAR = "escapeChar"; //$NON-NLS-1$

		public static final String AUTO_ESCAPE = "autoEscape"; //$NON-NLS-1$

		public static final String SOUND = "sound"; //$NON-NLS-1$

		public static final String ATTACHMENT = "attachment"; //$NON-NLS-1$

	}

	private class EscapeXMLCharMouseListener extends MouseAdapter {
		
		EscapeXMLCharacterPopupMenu popupMenu = null;
		
		public EscapeXMLCharMouseListener(TalkPane pPane) {
			popupMenu = new EscapeXMLCharacterPopupMenu(pPane);
		}
		
		public void mouseReleased(MouseEvent e) {
	        if (e.isPopupTrigger()) {
	        	popupMenu.show(e.getComponent(), e.getX(), e.getY());
	        }
		}
		
	}

	private class EscapeXMLCharacterPopupMenu extends JPopupMenu {

		public EscapeXMLCharacterPopupMenu(TalkPane pPane) {
			JMenuItem userEditMenuItem = new JMenuItem("LANSLIM.218"); //$NON-NLS-1$
	        userEditMenuItem.addActionListener(pPane);
	        userEditMenuItem.setActionCommand(TalkPaneActionCommand.ESCAPE_CHAR);
	        add(userEditMenuItem);
		}
	}
	
	private void escapeXMLChar() {
		
		int start = 0;
		int end = newMessageArea.getText().length();
		int pos = newMessageArea.getCaret().getDot();
		
		if (newMessageArea.getSelectedText() != null) {
			start = newMessageArea.getSelectionStart();
			end = newMessageArea.getSelectionEnd();
			pos = end;
		}
		String before = newMessageArea.getText().substring(0, start);
		String after = newMessageArea.getText().substring(end);
		String lBeforeReplace = newMessageArea.getText().substring(start, end);
		int count = 0;
		int from = 0;
		int last = 0;
		while (from < lBeforeReplace.length()) {
			last = lBeforeReplace.indexOf('<', from);
			if (last != -1) {
				count = count + 1;
				from = last + 1;
			}
			else {
				break;
			}
		}
		newMessageArea.setText(before + lBeforeReplace.replaceAll("<", "&lt;") + after); //$NON-NLS-1$ //$NON-NLS-2$
		newMessageArea.setCaretPosition(pos + + count * 3);
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		TalkPane pane = new TalkPane();
		JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.addTab("title1", new ImageIcon(ClassLoader.getSystemResource("icon/0.png")), pane, "tip1");
		tabbedPane.addTab("title2", new ImageIcon(ClassLoader.getSystemResource("icon/0.png")), new TalkPane(), "tip2");
		frame.add(tabbedPane);
		frame.setVisible(true);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		new Thread(new TapFlicker(tabbedPane, 1)).start();
	}
	
	static class TapFlicker implements Runnable {

		private JTabbedPane tabbedPane;
		private int index;
		public boolean stop = false;

		public TapFlicker(JTabbedPane tabbedPane, int i) {
			this.tabbedPane = tabbedPane;
			this.index = i;
		}

		@Override
		public void run() {
//			Timer timer = new Timer();
//			timer.schedule(new TapFlickerTask(), 1000);
//			tabbedPane.getTabComponentAt(index).addMouseListener(new MouseAdapter() {
//
//				@Override
//				public void mouseClicked(MouseEvent e) {
//					super.mouseClicked(e);
//					stop = true;
//					
//				}
//				
//			});
			while(tabbedPane.getSelectedIndex()!=index) {
				tabbedPane.setBackgroundAt(index, Color.YELLOW);
//				tabbedPane.setForegroundAt(index, Color.BLUE);
				tabbedPane.setToolTipTextAt(index, "New Message");
				try {
					Thread.currentThread().sleep(1000);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				tabbedPane.setBackgroundAt(index, null);
//				tabbedPane.setForegroundAt(index, Color.CYAN);
				try {
					Thread.currentThread().sleep(800);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}
		}
		
	}
	
}
